<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Video extends Model
{
    // 表名
    protected $name = 'video';


    public function getVideoList($where = [], $user_id = 0)
    {
        $list = $this->where(function ($query) use ($where) {
            if (isset($where['video_id']) && $where['video_id']){
                $query->whereIn('id', $where['video_id']);
            }
            if (isset($where['status']) && $where['status']){
                $query->where('status', $where['status']);
            }
        })
        ->field('id as video_id,name,label,image,money,status')
        ->select();
        foreach ($list as $key => $value) {
            if ($value['label']) {
               $list[$key]['label'] = explode(',', $value['label']);
            }else{
                $list[$key]['label'] = [];
            }
            if ($value['status'] == '2') {
                $user_video = Db::name('user_video')->where(['user_id' => $user_id,'video_id' => $value['video_id'],'buy_is' => '1'])->where('end_time' ,'>',time())->find();
                $list[$key]['buy_is'] = !empty($user_video) ? 1:0; # 1已购买0=未购买
            }else{
                # 免费视频 状态直接是已购买
                $list[$key]['buy_is'] = 1;
            }
        }
        return $list;
    }


  
}